package cn.xiaomubiao.oschine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.util.UIUtils;

/**
 * 详情页面的activity的基类
 *
 * @author 张伟斌 付东辉
 * @version 1.0
 * @date 2017/4/5
 */

public class MyInfoBaseActivity extends AppCompatActivity {

    public onRightTopViewListener listener; // 右上角按钮的回调接口对象

    @BindView(R.id.ib_back)
    ImageButton ibBack;
    @BindView(R.id.tv_detail_title)
    TextView tvDetailTitle;
    @BindView(R.id.ib_refresh)
    ImageButton ibRefresh;
    @BindView(R.id.fl_baseDetailFragment)
    FrameLayout flBaseDetailFragment;
    private Intent intent;

    // @BindView(R.id.info_activity_toolbar)
    // Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_myinfocollection);
        setContentView(R.layout.base_activity_detail);
        ButterKnife.bind(this);

        // 获得开启activity时传递过来的参数，用于替换fragment。
        intent = getIntent();

        if (intent == null) {
            return;

        }
        // Fragment全路径类名
        String className = intent.getStringExtra("className");
        // 用于拼接url的id
        int id = intent.getIntExtra("id", 0);
        // 标题栏中的文本
        String idString = intent.getStringExtra("idString");
        String tittle = intent.getStringExtra("title");
        // 右上角显示的图片资源id
        int imgId = getImageId();

        // 设置数据
        // mToolbar.setTitle(tittle);
        // 设置标题文本
        tvDetailTitle.setText(tittle);
        // 设置右上角图片
        if (imgId != 0) {
            ibRefresh.setImageResource(imgId);
        }

        // setSupportActionBar(mToolbar);
        // mToolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        //        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                finish();
        //            }
        //        });

        if (className == null) {
            return;
        }

        // 进行fragment视图的替换显示
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("idString",idString);
        bundle.putString("tag",intent.getStringExtra("tag"));
        Fragment fragment = Fragment.instantiate(UIUtils.getContext(), className, bundle);
        if (fragment == null) {
            return;
        }

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        //ft.replace(R.id.collection_activity, fragment);
        ft.replace(R.id.fl_baseDetailFragment, fragment);

        ft.commit();
    }

    private int getImageId() {
        // 右上角显示的图片资源id
        int imgId = intent.getIntExtra("imgId", 0);
        return imgId;

    }

    @OnClick({R.id.ib_back, R.id.ib_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;

            case R.id.ib_refresh:
                if (listener != null) {
                    listener.click();

                }

                break;
        }
    }

    // 点击事件的回调接口
    public interface onRightTopViewListener {
        void click();

    }

    // 设置监听器对象
    protected void setOnRightTopViewListener(onRightTopViewListener listener) {
        this.listener = listener;

    }

}
